package com.antler.luajavalibrary.luaext.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.antler.luajavalibrary.application.MyApplication;

public class UI {
    protected static final String TAG = "antler_" + UI.class.getSimpleName();

    // 组件类型
    public static class Type {
        public static final int UNKNOWN = -1;
        public static final int CONTAINER = 0;
        public static final int DIV = 1;
        public static final int TEXT = 2;
        public static final int IMAGE = 3;
        public static final int SCROLLER = 4;
        public static final int LIST = 5;
        public static final int SWITCH = 10;
        public static final int INPUT = 11;
        public static final int TEXTAREA = 12;
        public static final int VIDEO = 13;
        public static final int INDICATOR = 14;
        public static final int WEB = 17;
        public static final int LOADING = 18;
    }

    public static class ACTION {
        public static final int APPEAR = 0;
        public static final int DISAPPER = 1;
        public static final int CLICK = 2;
        public static final int LONG_PRESS = 3;
        public static final int SWIPE = 4;
    }

    public static class PSEUDO {
        public static final String ACTIVE = "active";
        public static final String FOCUS = "focus";
        public static final String ENABLED = "enabled";
        public static final String DISABLED = "disabled";
    }

    public static class LayoutParams {
        public static final int DEFAULT_WIDTH = 750;
    }

    private static UI g_instance = null;

    public static UI getInstance() {
        if (g_instance == null) {
            g_instance = new UI();
        }
        return g_instance;
    }

    protected Handler mMainUIHandler;
    protected Handler mHandler;
    protected WindowManager mWindowManager;
    protected Context mContext;

    public static void toast(final String msg, final int length) {
        UI.getInstance().asyncPost(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MyApplication.getContext(), msg, length).show();
            }
        });
    }

    private UI() {
        mContext = MyApplication.getContext();
        mMainUIHandler = new Handler(Looper.getMainLooper());
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    }

    public WindowManager getWindowManager() {
        return mWindowManager;
    }

    public boolean asyncPost(Runnable r) {
        boolean ret = true;
        if (mMainUIHandler.getLooper().isCurrentThread()) {
            r.run();
        } else {
            ret = mMainUIHandler.post(r);
        }
        return ret;
    }

    public boolean syncPost(final Runnable r) {
        boolean ret = true;
        if (mMainUIHandler.getLooper().isCurrentThread()) {
            r.run();
        } else {
            // 延迟为了保证先等待后执行
            ret = mMainUIHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    synchronized (UI.this) {
                        r.run();
                        UI.this.notify();
                    }
                }
            }, 20);

            synchronized (UI.this) {
                try {
                    UI.this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret;
    }

    public boolean asyncPostNO(Runnable r) {
        return mHandler.post(r);
    }
}
